package com.hsnn.medstgmini.sys.service.impl;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.base.dict.dao.DicAreaDao;
import com.hsnn.medstgmini.base.dict.model.DicArea;
import com.hsnn.medstgmini.base.std.dao.StdCompinfoDao;
import com.hsnn.medstgmini.base.std.dao.StdCompinfoDetailDao;
import com.hsnn.medstgmini.base.std.dao.StdCompinfoLogDao;
import com.hsnn.medstgmini.base.std.dao.StdHospInfoDao;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.HospInfoDetail;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.base.std.model.StdCompinfoDetail;
import com.hsnn.medstgmini.base.std.model.StdCompinfoDetailLog;
import com.hsnn.medstgmini.base.std.model.StdCompinfoLog;
import com.hsnn.medstgmini.sys.service.SysOrgInfoManager;
import com.hsnn.medstgmini.util.DateUtil;

@Service
public class SysOrgInfoManagerImpl implements SysOrgInfoManager {

	@Autowired
	private StdHospInfoDao hospInfoDao;

	@Autowired
	private StdCompinfoDao compinfoDao;

	@Autowired
	private StdCompinfoLogDao compinfoLogDao;

	@Autowired
	private StdCompinfoDetailDao compinfoDetailDao;

	@Autowired
	private DicAreaDao areaDao;

	/**
	 * @category 保存医疗机构信息
	 * @date 2015年7月1日
	 * @param hospInfo
	 */
	@Transactional
	public void saveHospOrgInfo(HospInfo hospInfo) {
		if (hospInfo != null) {
			HospInfoDetail detail = hospInfo.getHospInfoDetail();
			String hospId = hospInfo.getHospId();
			HospInfo hi = hospInfoDao.getHospInfoById(hospId);
			if (hi != null) {
				HospInfoDetail hid = hi.getHospInfoDetail();
				if (hid != null && detail != null) {
					hid.setContactName(detail.getContactName());
					hid.setContactIdnum(detail.getContactIdnum());
					hid.setContactTelephone(detail.getContactTelephone());
					hid.setContactHandphone(detail.getContactHandphone());
					hid.setContactQQ(detail.getContactQQ());
					hid.setContactEmail(detail.getContactEmail());
					hid.setContactPos(detail.getContactPos());
					hid.setUpdUser(hospInfo.getUpdUser());
					hid.setUpdDateTime(new Date());
					// 更新基础库并记录日志
					hospInfoDao.saveHospInfo(hi, 1);
				}
			}
		}
	}

	/**
	 * @category 保存企业信息
	 * @date 2015年7月1日
	 * @param compInfo
	 */
	@Transactional
	public void saveCompOrgInfo(StdCompinfo compInfo) {
		if (compInfo != null) {
			StdCompinfoDetail detail = compInfo.getStdCompinfoDetail();
			String compId = compInfo.getCompId();
			List<StdCompinfo> list = compinfoDao.queryStdCompinfoById(compId);
			if (list != null && !list.isEmpty()) {
				StdCompinfo ci = list.get(0);
				if (ci != null) {
					String areaId = ci.getDicArea().getAreaid();
					DicArea area = areaDao.getDicAreaById(areaId);
					StdCompinfoDetail cid = ci.getStdCompinfoDetail();
					if (cid != null && detail != null) {
						ci.setDicArea(area);
						cid.setContactName(detail.getContactName());
						cid.setContactIdnum(detail.getContactIdnum());
						cid.setContactTelephone(detail.getContactTelephone());
						cid.setContactHandphone(detail.getContactHandphone());
						cid.setContactQq(detail.getContactQq());
						cid.setContactEmail(detail.getContactEmail());
						cid.setContactPos(detail.getContactPos());
						cid.setUpdUser(compInfo.getUpdUser());
						cid.setUpdDatetime(DateUtil.getTimeStamp());
						// 更新基础库并记录日志
						compinfoDao.addAndEdit(ci);
						cid.setCompId(ci.getCompId());
						compinfoDetailDao.addAndEdit(cid);
						// 入日志表
						StdCompinfoLog compLog = new StdCompinfoLog();
						StdCompinfoDetailLog detailLog = new StdCompinfoDetailLog();
						BeanUtils.copyProperties(ci, compLog);
						BeanUtils.copyProperties(cid, detailLog);
						compinfoLogDao.add(compLog);
						detailLog.setLogId(compLog.getLogId());
						compinfoDetailDao.add(detailLog);
					}
				}
			}
		}
	}
}
